package com.ruoyi.plus.pub.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.plus.pub.domain.PubResources;
import com.ruoyi.plus.pub.service.IPubResourcesClassService;
import com.ruoyi.plus.pub.service.IPubResourcesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * 资源Controller
 */
@RestController
@RequestMapping("/pub/resources")
public class PubResourcesController extends BaseController {
    @Autowired
    private IPubResourcesService pubResourcesService;

    @Autowired
    private IPubResourcesClassService pubResourcesClassService;

    /**
     * 查询资源列表
     */
    @PreAuthorize("@ss.hasPermi('pub:resources:list')")
    @GetMapping("/list")
    public TableDataInfo list(PubResources pubResources) {
        startPage();
        if (pubResources != null && pubResources.getResourcesClassId() != null && pubResources.getResourcesClassId() == 0) {
            pubResources.setResourcesClassId(null);
        }
        List<PubResources> list = pubResourcesService.selectPubResourcesList(pubResources);
        return getDataTable(list);
    }

    /**
     * 导出资源列表
     */
    @PreAuthorize("@ss.hasPermi('pub:resources:export')")
    @Log(title = "资源导出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PubResources pubResources) {
        List<PubResources> list = pubResourcesService.selectPubResourcesList(pubResources);
        ExcelUtil<PubResources> util = new ExcelUtil<>(PubResources.class);
        return util.exportExcel(list, "resources");
    }

    /**
     * 获取资源详细信息
     */
    @PreAuthorize("@ss.hasPermi('pub:resources:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(pubResourcesService.getById(id));
    }

    /**
     * 新增资源
     */
    @PreAuthorize("@ss.hasPermi('pub:resources:add')")
    @Log(title = "资源添加或修改", businessType = BusinessType.INSERT)
    @PostMapping("/addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody PubResources pubResources) {
        pubResourcesService.addOrUpdateResources(pubResources);
        return AjaxResult.success();
    }

    @PostMapping("/checkResources")
    public AjaxResult checkResources(@RequestBody PubResources pubResources) {
        List<PubResources> resources = pubResourcesService.selectPubResourcesList(pubResources);
        return AjaxResult.success(resources.size() > 0);
    }

    /**
     * 删除资源(未删除磁盘资源文件)
     */
    @PreAuthorize("@ss.hasPermi('pub:resources:remove')")
    @Log(title = "资源删除", businessType = BusinessType.DELETE)
    @PostMapping("/del/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) throws IOException {
        pubResourcesService.removeResources(ids);
        return AjaxResult.success("资源删除成功！");
    }

}
